package fr.umlv.ig.metroView.confView;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import fr.umlv.ig.adapter.FilterModel;
import fr.umlv.ig.adapter.StationTableModel;
import fr.umlv.ig.adapter.StationTableRenderer;
import fr.umlv.ig.model.MetroModelImpl;
import fr.umlv.ig.model.Station;

public class TableStationConfView {
	
	public static JPanel getTableStationPanel(final MetroModelImpl model) {
		JPanel panel = new JPanel(new BorderLayout());
		final FilterModel filterModel = new FilterModel(model);
		
		final StationTableModel tableModel = new StationTableModel(filterModel);	
		final JTable table = getTable(tableModel);
	
	
		JPanel panelNorth = new JPanel();

		JLabel label = new JLabel("Search : ");
		panelNorth.add(label);
		JTextField textField = getTextSearch(tableModel);
		textField.setPreferredSize(new Dimension(200,20));
		panelNorth.add(textField);
		
		
		panel.add(new JScrollPane(table));
		panel.add(panelNorth,BorderLayout.NORTH);
		return panel;
	}
	
	private static JTextField getTextSearch(final StationTableModel tableModel) {
		JTextField textField = new JTextField("");
		textField.addCaretListener(new CaretListener() {
			@Override
			public void caretUpdate(CaretEvent e) {
				String s = ((JTextField)e.getSource()).getText();
				tableModel.filtre(s);

			}
		});		
		return textField;
	}
	
	
	private static JTable getTable(final StationTableModel tableModel) {
		final JTable table = new JTable( tableModel);	
		table.setDefaultRenderer(Object.class, StationTableRenderer.METROTABLECELLRENDERER);
		table.setDefaultEditor(Boolean.class, new DefaultCellEditor(new JComboBox(new Boolean[]{true,false})));
		
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				tableModel.selected((Station)(table.getValueAt(table.getSelectedRow(), 1)));
			}
		});
		initTable(table);
		return table;
	}
	

	
	public static JScrollPane getTableStation(final JTable table) {
		final StationTableModel tableModel = (StationTableModel) table.getModel();
		table.setDefaultRenderer(Object.class, StationTableRenderer.METROTABLECELLRENDERER);
		table.setDefaultEditor(Boolean.class, new DefaultCellEditor(new JComboBox(new Boolean[]{true,false})));

		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Station station = (Station ) (tableModel.getValueAt(table.getSelectedRow(), StationTableModel.colStation));
				tableModel.selected(station);
			}
		});
		
		JScrollPane scrollPane = new JScrollPane(table); 
		scrollPane.setPreferredSize(new Dimension(250, 250));
		return scrollPane;
	}
	
	
	private static void initTable(JTable table) {
		TableColumnModel modelColumn = table.getColumnModel();
		TableColumn column = modelColumn.getColumn(0);


		column.setMaxWidth(50);
		column.setMinWidth(30);
		column.setPreferredWidth(40);
		
		
		column = modelColumn.getColumn(1);

		column.setWidth(180);
		column.setMaxWidth(200);
		column.setMinWidth(150);
		column.setPreferredWidth(190);
		
		column = modelColumn.getColumn(2);
		column.setMinWidth(7);
		column.setPreferredWidth(15);
	}
}
